package initialize

import (
	"cron_table_admin/global"
	"gorm.io/gorm"
	"time"

	"gorm.io/driver/mysql"
)

func InitDbHelp() {
	mysqlConf := global.SERVER_CONFIG.Mysql
	dsn := mysqlConf.Username + ":" + mysqlConf.Password + "@tcp(" + mysqlConf.Path + ")/" + mysqlConf.Dbname + "?" + mysqlConf.Config
	Db, err := gorm.Open(mysql.Open(dsn))
	sqlDB, err := Db.DB()
	if err != nil {
		panic(err.Error())
	}
	// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
	sqlDB.SetMaxIdleConns(10)

	// SetMaxOpenConns sets the maximum number of open connections to the database.
	sqlDB.SetMaxOpenConns(100)

	// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
	sqlDB.SetConnMaxLifetime(time.Hour)
	global.GORM_DB = Db
	global.LOGGER.Debug("init mysql success")
}
